package com.lmh.graphdisease.controller;

import com.lmh.graphdisease.entity.UserEntity;
import com.lmh.graphdisease.service.UserService;
import com.lmh.graphdisease.utils.ResultBean;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author lmh
 * @desc
 * @date 2023/3/12
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(path = "/login",method = RequestMethod.POST)
    public ResultBean login(@RequestParam(required = true)String username,@RequestParam(required = true)String password){
        UserEntity curUser = userService.findUserEntityByUsername(username);
        if(curUser == null){
            return ResultBean.fail(404,"未找到相应用户");
        }
        if(!Objects.equals(curUser.getPassword(), password)){
            return ResultBean.fail(405,"密码输入错误，请重新输入");
        }
//        if(curUser.getPassword() == password){
            return ResultBean.success(curUser);
//        }
    }

    @RequestMapping(path = "/register",method = RequestMethod.POST)
    public ResultBean register(@RequestParam(required = true)String name,@RequestParam(required = true)String username,@RequestParam(required = true)String password){
        UserEntity userEntity = new UserEntity();
        if(Objects.equals(name, "") || Objects.equals(username, "") || Objects.equals(password, "")){
            return ResultBean.fail(405,"有空值");
        }
        userEntity.setName(name);
        userEntity.setUsername(username);
        userEntity.setPassword(password);
        boolean b = userService.registerUser(userEntity);
        if(!b){
            return ResultBean.fail(505,"已有该账号的用户，请重新输入账号");
        }else {
            return ResultBean.success("创建成功");
        }
    }


}
